package peer.httpoutput;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;

import peer.tools.CharMap;
import peer.tools.Mdb;


public class HttpRequest implements Runnable{
	Socket socket;
	Thread mainThread;
	DataInputStream dis;
	DataOutputStream dos;
	InputStream is=null;
	OutputStream os=null;
	HttpProcessor hp;
	Mdb mdb;
	HashMap castMemberTable;
	boolean playMode=false;
	HttpRequest(Socket socket){
		this.socket=socket;
	}
	@SuppressWarnings({ "deprecation", "deprecation" })
	public void run() {
		try {
			is=socket.getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			os=socket.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		dis=new DataInputStream(is);
		dos=new DataOutputStream(os);
		String str="";
		String str2="";
		int length=0;
		A: while(true){
			long offset=0;
			boolean haveRange=false;
			try {
				str=dis.readLine();
				//System.out.println(str);
			} catch (IOException e1) {
				break;
			}
			if(str==null){
				break;
			}
			while(true){
				try {
					str2=dis.readLine();
					System.out.println(str2);
				} catch (IOException e) {
					break A;
				}
				
				if(str2!=null){
					if(str2.equals("")){

						break ;
					}
					for(String sss:peer.Config.playerStr){
						if(str2.startsWith(sss)){
							playMode=true;
						}
					}
					
					if(str2.length()>=13){
						String s5=str2.substring(0,5);
						if(s5.equals("Range")){
							int index2=str2.indexOf('=')+1;
							int index3=str2.indexOf('-');
							String offsets=str2.substring(index2,index3);
							offset=Long.parseLong(offsets);
							haveRange=true;
						}
					}
				}else{
					break;
				}
			}
			int n1=0;
			for(int j=0;j<str.length();j++){
				String s=str.substring(j,j+1);
				if(s.equals(".")){
					n1++;
				}
			}
			if(n1<2){
				break;
			}
			int index1=str.indexOf(".")+1;
			int index2=str.indexOf(".",index1);
			int index3=str.indexOf("/")+1;
			//System.out.println(str);
			String mds=null;
			if(str.length()>index3+32){
				mds=str.substring(index3,index3+32);
				for(int j=0;j<mds.length();j++){
					String s=mds.substring(j,j+1);
					if(!CharMap.charTable2.containsKey(s)){
						break A;
					}
				}
			}else{
				break A;
			}
			String lengthStr=str.substring(index1,index2);
			
			for(int j=0;j<lengthStr.length();j++){
				String s=lengthStr.substring(j,j+1);
				if(!CharMap.charTable3.containsKey(s)){
					break A;
				}
			}
			length=Integer.valueOf(lengthStr);
			byte[] md=peer.tools.Pmd52f.hexStringToByteArray(mds);
			mdb=new Mdb(md);

			String s2=null;
			if(haveRange){
				s2="HTTP/1.1 206 OK\r\n"+"Content-Range: bytes "+Long.toString(offset)+"-"+Long.toString(length-1)+"/"+Long.toString(length)+"\r\n"+"Content-Length: "+Long.toString(length-offset)+"\r\n"+"Server: HttpServer\r\n"+"Accept-Ranges: bytes\r\n"+"\r\n";
			}else{
				s2="HTTP/1.1 200 OK\r\n"+"Content-Length: "+Long.toString(length)+"\r\n"+"Server: HttpServer\r\n"+"Accept-Ranges: bytes\r\n"+"\r\n";
			}
			try {
				dos.write(s2.getBytes());
			} catch (IOException e) {
				break ;
			}
			@SuppressWarnings("unused")
			boolean b=peer.Config.shareFileMange.containsShareFile(mdb);
			if(peer.Config.isLive){
				if(peer.Config.httpFL&peer.Config.shareFileMange.containsShareFile(mdb)){
					File file=peer.Config.shareFileMange.getShareFile(mdb);
					peer.Config.httpThreadPool.execute(new HttpProcessorLF(socket, file, dos, dis, offset, length));
				}else{
					if(!peer.Config.httpProcessorMange.containsHttpProcessorGroup(mdb)){
						peer.Config.httpProcessorMange.creatHttpProcessorGroup(mdb);
					}
					HttpProcessorGroup hpg=(HttpProcessorGroup)peer.Config.httpProcessorMange.getHttpProcessorGroup(mdb);
					hp=hpg.creatHttpProcessor(playMode,socket, mdb, dos, dis, offset, length);
				}
			}else{
				if(peer.Config.httpFL&peer.Config.shareFileMange.containsShareFile(mdb)){
					File file=peer.Config.shareFileMange.getShareFile(mdb);
					peer.Config.httpThreadPool.execute(new HttpProcessorLF(socket, file, dos, dis, offset, length));
				}else{
					try {
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			offset=0;
			haveRange=false;
			break ;
		}
	}
	
	void tryPCache(){
		
	}
}
